<?php
if (!defined('ACCESS')) {
    exit('Access denied.');
}

class AdminProducts extends Base
{
    // 表名
    private static $table_name = 'yg_products';
    // 查询字段
    private static $columns = array('products_id', 'products_name', 'products_img', 'products_desc_images', 'products_sales_num', 'remaining_time', 'products_price', 'products_special_price', 'products_currency', 'products_desc', 'products_index', 'products_add_date');


    public static function getTableName()
    {
        return self::$table_name;
    }

    public static function getAllProducts()
    {
        $db = self::__instance();
        $list = $db->select(self::getTableName(), self::$columns);
        return $list;
    }

    public static function getProductById($pid)
    {
        $condition = array("products_id" => $pid);
        $db = self::__instance();
        $product = $db->get(self::getTableName(), self::$columns, $condition);
        if ($product) {
            return $product;
        }
        return array();
    }

    public static function update($where, $data)
    {
        $db = self::__instance();
        $rt = $db->update(self::getTableName(), $data, $where);
        return $rt;
    }


    public static function delete($where)
    {
        $db = self::__instance();
        $rt = $db->delete(self::getTableName(), $where);
        return $rt;
    }


    public static function replace($data)
    {
        $db = self::__instance();
        if (!is_array($data)) {
            return false;
        }
        $value_str = '';
        $i = 0;
        $sign = ',';
        foreach ($data as $key => $value) {

            if (strlen($value[1]) > 26) {
                $value[1] = substr($value[1], 0, 26);
            }

            if (strlen($value[9]) > 120) {
                $value[9] = substr($value[9], 0, 120);
            }

            $s1 = "'" . $value[0] . "'";
            $s2 = "'" . $value[1] . "'";
            $s3 = "'" . $value[2] . "'";
            $s4 = "'" . $value[3] . "'";
            $s5 = "'" . $value[4] . "'";
            $s6 = "'" . $value[5] . "'";
            $s7 = "'" . $value[6] . "'";
            $s8 = "'" . $value[7] . "'";
            $s9 = "'" . $value[8] . "'";
            $s10 = "'" . $value[9] . "'";
            $s11 = "'" . $value[10] . "'";
            $s12 = "'" . date('Y-m-d H:i:s') . "'";
            $i++;
            $value_str_s = '(' . $s1 . ',' . $s2 . ',' . $s3 . ',' . $s4 . ',' . $s5 . ',' . $s6 . ',' . $s7 . ',' . $s8 . ',' . $s9 . ',' . $s10 . ',' . $s11 . ',' . $s12 . ')';
            if ($i == count($data)) {
                $sign = '';
            }
            $value_str .= $value_str_s . $sign;
        }


        $sql = "REPLACE INTO yg_products (`products_id`,`products_name`,`products_img`,`products_desc_images`,`products_sales_num`,`remaining_time`,`products_price`,`products_special_price`,`products_currency`,`products_desc`,`products_index`,`products_add_date`)
        VALUES " . $value_str;

        $rt = $db->exec($sql);
        return $rt;

    }
}